package io.netty.test.main.websocket;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * Created by LuoYu on 2019/2/9.
 * web socket
 * 基于http
 */
public class WsChannelInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new HttpClientCodec()) // ws基于http协议
                .addLast(new ChunkedWriteHandler()) // 以“块”的方式写
                .addLast(new HttpObjectAggregator(8192)) // 将http请求或者响应分段
                .addLast(new WebSocketServerProtocolHandler("/path")) // 内置的ws协议 ws://localhost:6666/path
                .addLast(new WsTextFrameHandler());
    }
}
